package com.zhuoli.service.springboot.redis.repository.repository;

import com.zhuoli.service.redis.client.template.redis.RedisTemplate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;


@Component
@AllArgsConstructor
@Slf4j
public class RedisSetRepository {

    private RedisTemplate redisTemplate;

    public String getSetByRandom(String key) {
        List<String> d = redisTemplate.execute(jedis -> jedis.srandmember(key, 1));
        if (d.size() > 0) {
            return d.get(0);
        } else {
            log.warn("cannot get random member from {}", key);
            return "";
        }
    }

    public List<String> getSetByRandom(String key, Integer count) {
        return redisTemplate.execute(jedis -> jedis.srandmember(key, count));
    }

    public Set<String> getSetMembers(String key) {
        return redisTemplate.execute(jedis -> jedis.smembers(key));
    }
}
